# -*- coding: utf-8 -*-
 
import random
from base import UserCase, Runner, TestSuite

class Case(UserCase):
    def rand_aggre(self):
        return random.choice(['avg', 'max', 'min'])

    def create_metric(self):
        p = {
            'code': self.randchar(12),
            'name': self.randhan(6),
            'aggre': self.rand_aggre(),
            'column': self.randchar(12),
        }
        res = self.c.open('/analytics/metric/create', p)
        self.assertEqual(res['code'], 0)
        return p['code']


    def create_calculate_metric(self):
        p = {
            'code': self.randchar(12),
            'name': self.randhan(6),
            'expression': self.randchar(48),
        }
        res = self.c.open('/analytics/calculate-metric/create', p)
        self.assertEqual(res['code'], 0)
        return p['code']


    def create_dimension(self):
        p = {
            'code': self.randchar(12),
            'name': self.randhan(6),
            'column': self.randchar(12),
        }
        res = self.c.open('/analytics/dimension/create', p)
        self.assertEqual(res['code'], 0)
        return p['code']


    def create_fact(self):
        p = {
            'code': self.randchar(12),
            'name': self.randhan(6),
            'table': self.randchar(8),
        }
        res = self.c.open('/analytics/fact/create', p)
        self.assertEqual(res['code'], 0)
        return res['data']['id']


    def test_fact_resource_create_metric(self):
        p = {
            'code': self.create_metric(),
            'fact': self.create_fact(),
            'type': 'metric',
        }
        res = self.c.open('/analytics/fact-resource/create', p)
        self.assertEqual(res['code'], 0)


    def test_fact_resource_create_calculate_metric(self):
        p = {
            'code': self.create_calculate_metric(),
            'fact': self.create_fact(),
            'type': 'calculate_metric',
        }
        res = self.c.open('/analytics/fact-resource/create', p)
        self.assertEqual(res['code'], 0)


    def test_fact_resource_create_dimension(self):
        p = {
            'code': self.create_dimension(),
            'fact': self.create_fact(),
            'type': 'dimension',
        }
        res = self.c.open('/analytics/fact-resource/create', p)
        self.assertEqual(res['code'], 0)


    def test_fact_resource_list(self):
        p = { }
        res = self.c.open('/analytics/fact-resource/list', p)
        self.assertEqual(res['code'], 0)


    def test_fact_resource_list_filter(self):
        fact = self.create_fact()
        p = {
            'code': self.create_dimension(),
            'fact': fact,
            'type': 'dimension',
        }
        res = self.c.open('/analytics/fact-resource/create', p)
        self.assertEqual(res['code'], 0)

        res = self.c.open('/analytics/fact-resource/list', {'fact': fact})
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['count'], 1)

        p = {
            'code': self.create_metric(),
            'fact': fact,
            'type': 'metric',
        }
        res = self.c.open('/analytics/fact-resource/create', p)
        self.assertEqual(res['code'], 0)

        res = self.c.open('/analytics/fact-resource/list', {'fact': fact})
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['count'], 2)

        p = {
            'code': self.create_metric(),
            'fact': fact,
            'type': 'metric',
        }
        res = self.c.open('/analytics/fact-resource/create', p)
        self.assertEqual(res['code'], 0)

        res = self.c.open('/analytics/fact-resource/list', {'fact': fact})
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['count'], 3)

        p = {
            'code': self.create_calculate_metric(),
            'fact': fact,
            'type': 'calculate_metric',
        }
        res = self.c.open('/analytics/fact-resource/create', p)
        self.assertEqual(res['code'], 0)

        res = self.c.open('/analytics/fact-resource/list', {'fact': fact})
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['count'], 4)


    def test_fact_resource_metric_recode(self):
        p = {
            'code': self.randchar(12),
            'name': self.randhan(6),
            'aggre': self.rand_aggre(),
            'column': self.randchar(12),
        }
        res = self.c.open('/analytics/metric/create', p)
        self.assertEqual(res['code'], 0)
        metric_code = p['code']
        metric_id = res['data']['id']

        p = {
            'code': metric_code,
            'fact': self.create_fact(),
            'type': 'metric',
        }
        res = self.c.open('/analytics/fact-resource/create', p)
        self.assertEqual(res['code'], 0)
        id = res['data']['id']

        new_code = self.randchar(8)
        res = self.c.open('/analytics/metric/update', {'id': metric_id, 'code': new_code})
        self.assertEqual(res['code'], 0)

        res = self.c.open('/analytics/fact-resource/read', {'id': id})
        self.assertEqual(res['data']['code'], new_code)


    def test_fact_resource_calculate_metric_recode(self):
        p = {
            'code': self.randchar(12),
            'name': self.randhan(6),
            'expression': self.randchar(48),
        }
        res = self.c.open('/analytics/calculate-metric/create', p)
        self.assertEqual(res['code'], 0)
        calculate_metric_code = p['code']
        calculate_metric_id = res['data']['id']

        p = {
            'code': calculate_metric_code,
            'fact': self.create_fact(),
            'type': 'calculate_metric',
        }
        res = self.c.open('/analytics/fact-resource/create', p)
        self.assertEqual(res['code'], 0)
        id = res['data']['id']

        new_code = self.randchar(8)
        res = self.c.open('/analytics/calculate-metric/update', {'id': calculate_metric_id, 'code': new_code})
        self.assertEqual(res['code'], 0)

        res = self.c.open('/analytics/fact-resource/read', {'id': id})
        self.assertEqual(res['data']['code'], new_code)


    def test_fact_resource_dimension_recode(self):
        p = {
            'code': self.randchar(12),
            'name': self.randhan(6),
            'column': self.randchar(8),
        }
        res = self.c.open('/analytics/dimension/create', p)
        self.assertEqual(res['code'], 0)
        dimension_code = p['code']
        dimension_id = res['data']['id']

        p = {
            'code': dimension_code,
            'fact': self.create_fact(),
            'type': 'dimension',
        }
        res = self.c.open('/analytics/fact-resource/create', p)
        self.assertEqual(res['code'], 0)
        id = res['data']['id']

        new_code = self.randchar(8)
        res = self.c.open('/analytics/dimension/update', {'id': dimension_id, 'code': new_code})
        self.assertEqual(res['code'], 0)

        res = self.c.open('/analytics/fact-resource/read', {'id': id})
        self.assertEqual(res['data']['code'], new_code)


test_list = [
    'test_fact_resource_create_metric',
    'test_fact_resource_create_calculate_metric',
    'test_fact_resource_create_dimension',
    'test_fact_resource_list',
    'test_fact_resource_list_filter',
    'test_fact_resource_metric_recode',
    'test_fact_resource_calculate_metric_recode',
    'test_fact_resource_dimension_recode',
]
Suite = TestSuite([Case(t) for t in test_list])


if __name__ == '__main__':
    Runner.run(Suite)



